package com.cuit.controller;

import com.cuit.pojo.AcceptOrderData;
import com.cuit.pojo.Doctor;
import com.cuit.pojo.DoctorOrder;
import com.cuit.service.Impl.PatientServiceImpl;
import com.cuit.service.PatientService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.Session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class P_QueryServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(true);
        String department = req.getParameter("department");
        String doctor_name = req.getParameter("doctor");
        String category = req.getParameter("category");
        String sitting_time = req.getParameter("sitting_time");
        String order_number = req.getParameter("order_number");
        String week = req.getParameter("d_week");
        System.out.println(week);
        int[] d_week = new int[7];
        String[] d_sitting_time=new String[3];
        d_sitting_time[0]=sitting_time;
        d_week[0] = Integer.parseInt(String.valueOf(week.charAt(0)));
        System.out.println(d_sitting_time[0]+" "+order_number+" "+d_week[0]);
        Doctor doctor = new Doctor(doctor_name,department,category);
        DoctorOrder doctorOrder = new DoctorOrder(d_sitting_time, d_week,order_number);
        PatientService patientService = new PatientServiceImpl();
        List<AcceptOrderData> list = patientService.queryDoctor(doctor,doctorOrder);
        if(list==null||list.size()==0) {
            req.getSession().setAttribute("AcceptOrderData", null);
        } else{
            req.getSession().setAttribute("AcceptOrderData", list.get(0));
        }
//        System.out.println(list.get(0).getD_name());
        if(list==null){
            System.out.println("没有医生的相关信息！！！");
            ObjectMapper mapper=new ObjectMapper();
            String json=mapper.writeValueAsString(list);
            System.out.println(json);

            PrintWriter out=resp.getWriter();
            out.print(json);
        }else{
            ObjectMapper mapper=new ObjectMapper();
            String json=mapper.writeValueAsString(list);
            session.setAttribute("list",list);
            System.out.println(json);
            PrintWriter out=resp.getWriter();
            out.print(json);
            System.out.println("查找成功！！！");

        }
}

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
